#include "slist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void my_print(void *data);
void my_destroy(void *data);
int my_compare(node_t *n1, node_t *n2);

int main(int argc, char **argv)
{
    slist_t *s = NULL;
    int num = 0;
    int a = 0;

    slist_create(&s, sizeof(int), my_destroy);

    while (1)
    {
    op:
        my_scanf(op, "%d", &num);

        if (num == -1)
        {
            break;
        }

        slist_append(s, &num);
    }

    slist_traverse(s, my_print);

    a = 66;
    slist_insert_by_index(s, &a, 9);

    slist_traverse(s, my_print);

    slist_sort(s, my_compare);
    while (1);

    slist_traverse(s, my_print);

    slist_destroy(s);
    free(s);
    s = NULL;


    return 0;
}



void my_destroy(void *data)
{
    node_t *tmp = NULL;

    // 参数检查
    if (NULL == data)
    {
        perror("argument lawless...");
        return;
    }

    tmp = data;

    // 完全销毁节点
    free(tmp->data);
    tmp->data = NULL;
    free(tmp);
    tmp = NULL;


    return;
}

void my_print(void *data)
{
    node_t *tmp = NULL;

    // 参数检查
    if (NULL == data)
    {
        perror("argument lawless...");
        return;
    }

    tmp = data;

    // 打印节点内容
    printf("%d ", *(int *)(tmp->data));



    return;
}

int my_compare(node_t *n1, node_t *n2)
{

    // 参数检查
    if (NULL == n1 || NULL == n2)
    {
        perror("argument lawless...");
        goto err0;
    }

    if ((*(int *)(n1->data)) > (*(int *)(n2->data)))
    {
        return 1;
    }
    else if ((*(int *)(n1->data)) < (*(int *)(n2->data)))
    {
        return -1;
    }
    else 
    {
        return 0;
    }


    return 0;
err0:
    return 1234;
}